package Client;


import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.rmi.Naming;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import ServerApp.IServer;
import ServerApp.Meeting;
import ServerApp.Server;
import ServerApp.Tools;
import ServerApp.User;

// test

@SuppressWarnings("serial")
public class LoginFenster extends JFrame {

	public static String LOCALHOST = "127.0.0.1";
	
	private JLabel lLogin = new JLabel();
	private JLabel lUsername = new JLabel();
	private JLabel lPasswort = new JLabel();
	private JTextField tUsername = new JTextField();
	private JPasswordField tPassword = new JPasswordField();
	private JButton bLogin = new JButton();
	private JCheckBox chRemember = new JCheckBox();
	private JButton bRegister = new JButton();
	
	private String username;
	
	public LoginFenster() {
		super("Login");
		lLogin.setText("Login");
		lLogin.setFont(new Font("Arial", Font.BOLD, 20));
		lUsername.setText("Userame");
		lPasswort.setText("Passwort");
		chRemember.setText("Remebmer me");
		chRemember.setSelected(false);
		tUsername.setColumns(20);
		tPassword.setColumns(20);
		File f = new File("users");
		if(f.exists()){
			try {
				BufferedReader DataIn = new BufferedReader(new FileReader(f));
				byte[] bytes = DataIn.readLine().getBytes();
				for(int i=0; i<bytes.length; i++)
					bytes[i] -= 13;
				String[] strings = new String(bytes).split("@");
				tUsername.setText(strings[0]);
				tPassword.setText(strings[1]);
				chRemember.setSelected(true);
				
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
		bLogin.setText("Login");
		bLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bLoginAction();
			}
		});
		bRegister.setText("Register");
		
		this.getContentPane().setLayout(new GridBagLayout());
		this.getContentPane().add(lLogin, new GridBagConstraints(0,0,2,1,0.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.NONE,
				new Insets(5, 5, 5, 5),0,0));
		this.getContentPane().add(lUsername, new GridBagConstraints(0,1,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,
				new Insets(5, 3, 3, 3),0,0));
		this.getContentPane().add(tUsername, new GridBagConstraints(1,1,1,1,0.0,0.0,GridBagConstraints.EAST,GridBagConstraints.HORIZONTAL,
				new Insets(3, 3, 3, 5),0,0));
		this.getContentPane().add(lPasswort, new GridBagConstraints(0,2,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,
				new Insets(5, 3, 3, 3),0,0));
		this.getContentPane().add(tPassword, new GridBagConstraints(1,2,1,1,0.0,0.0,GridBagConstraints.EAST,GridBagConstraints.HORIZONTAL,
				new Insets(3, 3, 3, 5),0,0));
		this.getContentPane().add(bLogin, new GridBagConstraints(0,4,2,1,0.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.NONE,
				new Insets(5, 5, 5, 5),0,0));
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.pack();
		this.setResizable(false);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(screen.width/2 - this.getWidth()/2, screen.height/2 - this.getHeight()/2);
	}
	
	public void SetUserName(String username){
		this.username = username;
	}
	
	/**
	 * Connect to the server
	 */
	private void bLoginAction(){
		try{
			System.out.println("Login");
			String errorMsg = "";
			String userName = tUsername.getText();
			String password = new String(tPassword.getPassword());
			if(userName.isEmpty())
				errorMsg += "Enter Username ";
			if(password.isEmpty())
				errorMsg += "Enter Password ";
			if(!errorMsg.isEmpty()){
				Tools.errorMessage("Missing information", errorMsg);
				return;
			}
				
			IServer server = (IServer) Naming.lookup("//" + LOCALHOST + "/" + Server.SERVICE_NAME);
			User user = server.login(userName, password);
			if(user == null){
				Tools.errorMessage("Error", "Invalid user data");
				tPassword.setText("");
				return;
			}
			this.dispose();
//			ArrayList<Meeting> list = server.getMeetings();
			HauptFenster main = new HauptFenster(server, user);
			main.setVisible(true);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new LoginFenster().setVisible(true);
	}
}
